<template>
  <div class="form-single-fragment" style="position: relative;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini" label-position="right">
      <el-form-item label="文件名称" prop="task">
        <el-input class="input-item" v-model="ruleForm.task" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="上传附件" prop="files">
        <Upload v-model="ruleForm.accessoryList" fielState="11" :limit="10"></Upload>
      </el-form-item>
      <el-form-item style="padding: 20px 0;">
        <el-button size="mini" @click="onCancel(false)">取消</el-button>
        <el-button type="primary" size="mini" @click="submitForm()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getType } from '@/utils';
import { Administration } from '@/api/projectAdministration.js';
export default {
  name: 'Achievementsadd',
  props: {
    projectId: {
      type: String,
      default: () => {
        return ''
      }
    },
    achievement: {
      default: undefined
    }
  },
  data () {
    return {
      loading: false,
      isShowloading: false,
      fileLists: [],
      acceptStr: '.doc,.docx,.xls,.xlsx,.jpg,.png,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      ruleForm: {
        task: '',
        accessoryList: []
      },
      rules: {
        task: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  created () {
    console.log(this.achievement, 61)
    if (this.achievement) {
      this.ruleForm.task = this.achievement.task;
      this.ruleForm.id = this.achievement.id;
      let accessory = this.achievement?.accessoryList;
      this.ruleForm.accessoryList = getType(accessory).isString ? JSON.parse(accessory) : accessory;
    }
  },
  computed: {
    isEdit () {
      return this.achievement && this.achievement.id;
    }
  },
  methods: {
    /**
     * 提交表单
     */
    submitForm () {
      console.log(this.ruleForm)
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isShowloading = true;
          this.ruleForm.projectId = this.projectId
          if (this.isEdit) {
            let params = {
              ...this.ruleForm
            };
            return new Promise((resolve, reject) => {
              Administration.achievementupdate(this, params).then(res => {
                this.$message({
                  showClose: true,
                  message: '修改成功！',
                  type: 'success'
                });
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            });
          } else {
            let params = {
              ...this.ruleForm
            };
            return new Promise((resolve, reject) => {
              Administration.achievementadd(this, params).then(res => {
                this.$message({
                  showClose: true,
                  message: '添加成功！',
                  type: 'success'
                });
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel (isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess);
      }
    }
  }
}
</script>
